package com.highcom.controller;

import com.highcom.domain.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BookController {

    //book  string name  Double price

    @RequestMapping("/showPageDate1")
    public String showPageDate1(HttpServletRequest request){
        //使用原生request传递参数
        request.setAttribute("name","lilei");
        Book book = new Book();
        book.setName("水浒传");
        book.setPrice(120D);
        request.setAttribute("book",book);
        return "page";
    }

    @RequestMapping("/showPageDate2")
    public String showPageDate2(Model model){
        //使用Model传递参数
        Book book = new Book();
        book.setName("水浒传");
        book.setPrice(120D);
        model.addAttribute("name","lilei");
        model.addAttribute("book",book);
        return "page";
    }

    //使用ModelAndView传递参数，该对象还封装了页面
    @RequestMapping("/showPageDate3")
    public ModelAndView showPageDate3(ModelAndView modelAndView){
        Book book = new Book();
        book.setName("springMVC 从入门到精通");
        book.setPrice(120.6);
        modelAndView.addObject("book",book);
        modelAndView.addObject("name","Tom");

        //设置页面
        modelAndView.setViewName("page");
        return modelAndView;
    }
    // ModelAndView对象支持手工设定的转发，该设定不会启用前后缀的页面拼接格式
    @RequestMapping("/showPageDate4")
    public ModelAndView showPageDate4(ModelAndView modelAndView){
        modelAndView.setViewName("forward:/WEB-INF/page/page.jsp");
        return modelAndView;
    }
    // ModelAndView对象支持手工设定的重定向，该设定不会启用前后缀的页面拼接格式
    @RequestMapping("/showPageDate5")
    public ModelAndView showPageDate5(ModelAndView modelAndView){
        modelAndView.setViewName("redirect:page.jsp");
        return modelAndView;
    }

}
